package cn.danger.com.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

/**
 * Created by 俊 on 2016/12/30.
 */
public class DBHelper extends SQLiteOpenHelper {

    private static DBHelper helper;
    private static final String DB_NAME = "schedule.db";
    private static final int DB_VERSION = 1;
    private static final String SQL_CREATE_MEMO = "create table memo(id integer primary key autoincrement," +
            "objectId text,username text,title text,date text,content text" + ")";
    private static final String SQL_DROP_MEMO = "drop table if exists memo";
    private static final String SQL_CREATE_OPERATE = "create table operate(id integer primary key autoincrement,operate_id integer," +
            "username text,operate_table text,operate_objectId text,operate_type text" + ")";
    private static final String SQL_DROP_OPERATE = "drop table if exists operate";
    private static final String SQL_CREATE_SCHEDULE = "create table schedule(id integer primary key autoincrement," +
            "objectId text,username text,content text,date_start text,type text,remind integer," +
            "remind_time text,remind_ring text" + ")";
    private static final String SQL_DROP_SCHEDULE = "drop table if exists schedule";

    private DBHelper(Context context) {
        super(context, DB_NAME, null, DB_VERSION);
    }

    public static DBHelper getInstance(Context context) {
        if (helper == null) {
            synchronized (DBHelper.class) {
                if (helper == null) {
                    helper = new DBHelper(context);
                }
            }
        }
        return helper;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(SQL_CREATE_MEMO);
        db.execSQL(SQL_CREATE_OPERATE);
        db.execSQL(SQL_CREATE_SCHEDULE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL(SQL_DROP_MEMO);
        db.execSQL(SQL_DROP_OPERATE);
        db.execSQL(SQL_DROP_SCHEDULE);
        db.execSQL(SQL_CREATE_MEMO);
        db.execSQL(SQL_CREATE_OPERATE);
        db.execSQL(SQL_CREATE_SCHEDULE);
    }
}
